package config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * 和jdbc操作相关的配置类
 */
public class JdbcConfig {

    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String  url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;





    @Bean(value = "createJdbcTemplate")
    public JdbcTemplate createJdbcTemplate(){
        System.out.println(" 没有" );
        return new JdbcTemplate(createDataSource());
    }

    @Bean(value = "createJdbcTemplate")
    public JdbcTemplate createJdbcTemplate(@Autowired DataSource dataSource){
        System.out.println(" 有参数" );
        return new JdbcTemplate(dataSource);
    }


    @Bean
    public DataSource createDataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }



}
